package com.atguigu.gmall.config;

import com.alibaba.dubbo.config.*;
import com.atguigu.gmall.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MyDubboConfig {
    @Bean
    //代替”<dubbo:application...“
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig=new ApplicationConfig();
        applicationConfig.setName("boot-provider-666");
        return applicationConfig;
    }
    @Bean
    //代替<dubbo:registry...
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("192.168.80.130:2181,192.168.80.131:2181,192.168.80.132:2181");
        return registryConfig;
    }
    @Bean
    //代替<dubbo:protocol...
    public ProtocolConfig protocolConfig(){
        ProtocolConfig protocolConfig=new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20081);
        return protocolConfig;
    }
    @Bean
    //代替<dubbo:service
    public ServiceConfig<UserService> serviceConfig(UserService userService){
        ServiceConfig<UserService> serviceConfig=new ServiceConfig<>();
        serviceConfig.setInterface(UserService.class);
        serviceConfig.setRef(userService);
        //由于接口实现类已经使用"@Component"注解注册到ioc容器中了
        //所以这里方法形参里可以直接得到
        serviceConfig.setTimeout(2000);
        serviceConfig.setVersion("1.0.0");
        //配置每一个method的信息
        MethodConfig methodConfig = new MethodConfig();
        methodConfig.setName("getUserAddressList");
        methodConfig.setTimeout(1000);
        //将单个method的设置保存在serviceConfig里面
        List<MethodConfig> methodConfigs=new ArrayList<>();
        methodConfigs.add(methodConfig);
        serviceConfig.setMethods(methodConfigs);
        return serviceConfig;
    }
    //其他的配置以此类推
}
